package com.qianbao.qianbaobusiness.ui.cashier;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.androidbase.base.BaseActivity;
import com.baseandroid.utils.FormatUtil;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.DayBillDetailedBean;
import com.qianbao.qianbaobusiness.ui.help.HelpCenterActivity;
import com.qianbao.qianbaobusiness.ui.widget.RiseNumberTextView;

import java.math.BigDecimal;

public class BillingDetailsActivity extends BaseActivity {

    private FrameLayout toolbar_back;
    private FrameLayout toolbar_right;
    private RiseNumberTextView settlementAmountTv;
    private TextView receivablesSuccessTimeTv;
    private TextView submitSettlementTimeTv;
    private TextView cashAccountTimeTv;
    private TextView customerPaymentMoneyTv;
    private TextView counterFeeMoneyTv;
    private TextView settlementMoneyTv;
    private TextView billQuestionsTv;
    private TextView paymentMethodTv;
    private TextView orderNumberTv;
    private TextView balance_money_tv, customer_balance_money_tv;
    private TextView coupon_money_tv, coupon_balance_money_tv;
    private View view_money_line2;
    private int settlementId;

    private DayBillDetailedBean billDetailedBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_billing_details;
    }

    @Override
    protected void setupView() {
        toolbar_back = (FrameLayout) findViewById(R.id.toolbar_back);
        toolbar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolbar_right = (FrameLayout) findViewById(R.id.toolbar_right);
        toolbar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putCharSequence("sceneId", "2");
                Intent intent = new Intent();
                intent.putExtras(b);
                intent.setClass(BillingDetailsActivity.this, HelpCenterActivity.class);
                startActivity(intent);
            }
        });
        // 结算金额（元）
        settlementAmountTv = (RiseNumberTextView) findViewById(R.id.billing_settlement_amount_tv);
        // 收款成功
        receivablesSuccessTimeTv = (TextView) findViewById(R.id.receivables_success_time_tv);
        // 提交结算
        submitSettlementTimeTv = (TextView) findViewById(R.id.submit_settlement_time_tv);
        // 款项到账
        cashAccountTimeTv = (TextView) findViewById(R.id.cash_account_time_tv);
        // 客户支付金额
        customerPaymentMoneyTv = (TextView) findViewById(R.id.customer_payment_money_tv);
        // 支付手续费
        counterFeeMoneyTv = (TextView) findViewById(R.id.counter_fee_money_tv);
        // 结算金额
        settlementMoneyTv = (TextView) findViewById(R.id.settlement_money_tv);
        // 问题解答
        billQuestionsTv = (TextView) findViewById(R.id.bill_questions_tv);
        // 支付方式
        paymentMethodTv = (TextView) findViewById(R.id.payment_method_tv);
        // 订单号
        orderNumberTv = (TextView) findViewById(R.id.order_number_tv);
        // 原订单金额
        balance_money_tv = (TextView) findViewById(R.id.balance_money_tv);
        customer_balance_money_tv = (TextView) findViewById(R.id.customer_balance_money_tv);
        // 优惠金额
        coupon_money_tv = (TextView) findViewById(R.id.coupon_money_tv);
        coupon_balance_money_tv = (TextView) findViewById(R.id.coupon_balance_money_tv);
        view_money_line2 = findViewById(R.id.view_money_line2);
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
        billDetailedBean = (DayBillDetailedBean) getIntent().getSerializableExtra("billDetailedBean");
        if (billDetailedBean != null) {
            String settlementAmt = Double.parseDouble(billDetailedBean.getSettlementAmt() + "") / 100 + "";
            //        setAmountMoneyText(settlementAmountTv, settlementAmt);
            settlementAmountTv.setText(FormatUtil.getMoneyFormat(settlementAmt));
            receivablesSuccessTimeTv.setText(billDetailedBean.getTradeDate());
            submitSettlementTimeTv.setText(billDetailedBean.getTradeDate()
                    .substring(0, 11) + "\n" + "24:00:00");
            String changeTime1 = FormatUtil.changeTime1(billDetailedBean.getSettleDate(), "yyyy-MM-dd");
            cashAccountTimeTv.setText("预计" + changeTime1 + "\n24:00:00" + "前");

            customerPaymentMoneyTv.setText(getPaymentType(billDetailedBean.getTradeAmt()));
            counterFeeMoneyTv.setText(getPaymentType(billDetailedBean.getFeeAmt()));
            settlementMoneyTv.setText(getPaymentType(billDetailedBean.getSettlementAmt()));
            paymentMethodTv.setText("支付方式：" + setPayment(billDetailedBean.getChkAccountType()));
            if (!TextUtils.isEmpty(billDetailedBean.getOrderNo())) {
                orderNumberTv.setText(billDetailedBean.getOrderNo());
            } else {
                orderNumberTv.setText(billDetailedBean.getReferNo());
            }
        }
    }

    private String getPaymentType(long amt) {
        String settlementAmt = Double.parseDouble(amt + "") / 100 + "";
        return getResources().getString(R.string.rmb) + FormatUtil.getMoneyFormat(settlementAmt);
    }

    private String setPayment(int type) {
        switch (type) {
            case 1:
                String kindStr = null;
                if (TextUtils.isEmpty(billDetailedBean.getCardKind())) {
                    kindStr = "";
                } else if (billDetailedBean.getCardKind().equals("CC")) {
                    kindStr = "-贷记卡";
                } else if (billDetailedBean.getCardKind().equals("DC")) {
                    kindStr = "-借记卡";
                }
                balance_money_tv.setVisibility(View.GONE);
                coupon_money_tv.setVisibility(View.GONE);
                customer_balance_money_tv.setVisibility(View.GONE);
                coupon_balance_money_tv.setVisibility(View.GONE);
                view_money_line2.setVisibility(View.GONE);
                return "刷卡" + kindStr;

            case 2:
                balance_money_tv.setVisibility(View.GONE);
                coupon_money_tv.setVisibility(View.GONE);
                customer_balance_money_tv.setVisibility(View.GONE);
                coupon_balance_money_tv.setVisibility(View.GONE);
                view_money_line2.setVisibility(View.GONE);
                return "支付宝支付";

            case 3:
                balance_money_tv.setVisibility(View.GONE);
                coupon_money_tv.setVisibility(View.GONE);
                customer_balance_money_tv.setVisibility(View.GONE);
                coupon_balance_money_tv.setVisibility(View.GONE);
                view_money_line2.setVisibility(View.GONE);
                return "微信支付";

            case 4:
                balance_money_tv.setVisibility(View.VISIBLE);
                coupon_money_tv.setVisibility(View.VISIBLE);
                customer_balance_money_tv.setVisibility(View.VISIBLE);
                coupon_balance_money_tv.setVisibility(View.VISIBLE);
                view_money_line2.setVisibility(View.VISIBLE);
                customer_balance_money_tv.setText(getPaymentType(billDetailedBean.getOrderAmt()));
                coupon_balance_money_tv.setText(getPaymentType(billDetailedBean.getDiscountAmt()));
                return "钱包生活买单";

            default:
                return "";
        }
    }

    private void setAmountMoneyText(final RiseNumberTextView textView, String balance) {
        if (!TextUtils.isEmpty(balance) && !balance.equals("0.00")) {
            BigDecimal bd = new BigDecimal(balance);
            if (Float.parseFloat(balance) < 1) {
                balance = FormatUtil.parseMoney(",###,###0.00", bd);
            } else {
                balance = FormatUtil.parseMoney(",###,###.00", bd);
            }
            final String b = balance;
            if (!TextUtils.isEmpty(b) && b.equals("0.00")) {
                textView.setText("0.00");
                return;
            }
            textView.withNumberDouble(b, RiseNumberTextView.INCREASE);
            textView.setOnEnd(new RiseNumberTextView.EndListener() {
                @Override
                public void onEndFinish() {
                    textView.setText(b);
                }
            });
            // 设置动画播放时间
            textView.setDuration(1000);
            // 开始播放动画
            textView.start();
        } else {
            balance = "0.00";
            textView.setText(balance);
        }
    }

}
